﻿Imports WeatherServices
Imports System.Xml
Imports System.IO

Public Class searchlocation
    Dim locs As New WxLocationCollection

    Private Sub btn_search_Click(sender As Object, e As EventArgs) Handles btn_search.Click
        For Each Loc As WxLocation In Service.SearchLocation(txt_argument.Text, Form1.locale)
            Lbox1.Items.Add(Loc.City & " | " & Loc.Country & " | " & Loc.LocationID)
            locs.Add(Loc)
        Next
    End Sub

    Public Sub SaveLocation(ByVal path As String, ByVal wxloc As WxLocation)
        Dim doc As New XmlDocument

        If File.Exists(path) Then

            doc.Load(path)

            Dim exists As Boolean

            For Each nod As XmlNode In doc.SelectNodes("/locations/location")
                If nod.SelectSingleNode("parentId").InnerText.Contains(wxloc.LocationID) Then
                    exists = True
                End If
            Next

            If exists = True Then
                MsgBox("Location allready exists!")
                Exit Sub
            Else
                Dim root As XmlElement = doc.SelectSingleNode("locations")

                Dim loc As XmlElement = doc.CreateElement("location")

                Dim city As XmlElement = doc.CreateElement("city")
                city.InnerText = wxloc.City

                Dim country As XmlElement = doc.CreateElement("country")
                country.InnerText = wxloc.Country

                Dim lat As XmlElement = doc.CreateElement("latitude")
                lat.InnerText = wxloc.Latitude

                Dim lng As XmlElement = doc.CreateElement("longitude")
                lng.InnerText = wxloc.Longitude

                Dim woeid As XmlElement = doc.CreateElement("parentId")
                woeid.InnerText = wxloc.LocationID

                Dim state As XmlElement = doc.CreateElement("state")
                state.InnerText = wxloc.State

                Dim statename As XmlElement = doc.CreateElement("statename")
                statename.InnerText = wxloc.StateName

                Dim sel As XmlElement = doc.CreateElement("selected")
                If chk_sel.Checked = True Then
                    sel.InnerText = "true"
                Else
                    sel.InnerText = "false"
                End If

                loc.AppendChild(city)
                loc.AppendChild(country)
                loc.AppendChild(lat)
                loc.AppendChild(lng)
                loc.AppendChild(woeid)
                loc.AppendChild(state)
                loc.AppendChild(statename)
                loc.AppendChild(sel)

                root.AppendChild(loc)
            End If
        Else
            Dim root As XmlElement = doc.CreateElement("locations")

            Dim loc As XmlElement = doc.CreateElement("location")

            Dim city As XmlElement = doc.CreateElement("city")
            city.InnerText = wxloc.City

            Dim country As XmlElement = doc.CreateElement("country")
            country.InnerText = wxloc.Country

            Dim lat As XmlElement = doc.CreateElement("latitude")
            lat.InnerText = wxloc.Latitude

            Dim lng As XmlElement = doc.CreateElement("longitude")
            lng.InnerText = wxloc.Longitude

            Dim woeid As XmlElement = doc.CreateElement("parentId")
            woeid.InnerText = wxloc.LocationID

            Dim state As XmlElement = doc.CreateElement("state")
            state.InnerText = wxloc.State

            Dim statename As XmlElement = doc.CreateElement("statename")
            statename.InnerText = wxloc.StateName

            Dim sel As XmlElement = doc.CreateElement("selected")
            If chk_sel.Checked = True Then
                sel.InnerText = "true"
            Else
                sel.InnerText = "false"
            End If

            loc.AppendChild(city)
            loc.AppendChild(country)
            loc.AppendChild(lat)
            loc.AppendChild(lng)
            loc.AppendChild(woeid)
            loc.AppendChild(state)
            loc.AppendChild(statename)
            loc.AppendChild(sel)

            root.AppendChild(loc)

            doc.AppendChild(root)
        End If

        doc.Save(path)
    End Sub

    Private Sub btn_add_Click(sender As Object, e As EventArgs) Handles btn_add.Click
        SaveLocation(Application.StartupPath & "\locations.xml", locs(Lbox1.SelectedIndex))

        Locations.Lbox1.Items.Add(locs(Lbox1.SelectedIndex).City)
        Locations.wxlocs.Add(locs(Lbox1.SelectedIndex))

        Me.Close()
    End Sub

    Private Sub btn_cancel_Click(sender As Object, e As EventArgs) Handles btn_cancel.Click
        Me.Close()
    End Sub
End Class